//
//  PasswordView.m
//  汽车大灯色温蓝牙
//
//  Created by dfwx on 2020/9/10.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "PasswordView.h"

// 输入密码的位数
static const int boxCount = 6;
// 输入方格的边长
static const CGFloat boxWH = 40;
@interface PasswordView()
@property (strong, nonatomic) NSMutableArray *boxes;
// 占位编辑框(这样就点击密码格以外的部分,可以弹出键盘)
@property (weak, nonatomic) UITextField *contentTextField;
@property (nonatomic, assign) CGFloat boxWidth;
@property (nonatomic, strong) UILabel *headLabel;

@end

@implementation PasswordView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        [self setUpContentView];
    }
    return self;
}

- (void)setUpContentView
{
    UILabel *headLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, self.frame.size.width-30, self.frame.size.height-60)];
    self.headLabel = headLabel;
    headLabel.text = @"请输入六位数修改密码\n为了防止别人误连接";
    headLabel.numberOfLines = 0;
    headLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightMedium];
    headLabel.textColor = UIColorFromHex(0x333333);
    headLabel.textAlignment = NSTextAlignmentCenter;
    headLabel.backgroundColor = [UIColor clearColor];
    headLabel.adjustsFontSizeToFitWidth = YES;
    
    [self addSubview:headLabel];
    
//    UILabel *contentLabel = [UILabel alloc]initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
    
    // 密码格之间的间隔
    CGFloat margin = 10;
    CGFloat textFieldHeigh = self.frame.size.height-CGRectGetMaxY(headLabel.frame);
    self.boxWidth = (self.frame.size.width-margin*(boxCount+1))/boxCount;
    
    UITextField *contentField = [[UITextField alloc] initWithFrame:CGRectMake(0, self.frame.size.height-60, self.frame.size.width, 60)];
    self.contentTextField = contentField;
    contentField.placeholder = @"请输入六位密码";
    contentField.hidden = YES;
    contentField.keyboardType = UIKeyboardTypeNumberPad;
    [contentField addTarget:self action:@selector(txchange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:contentField];
    
    
    for (int i = 0; i < boxCount; i++)
    {
        CGFloat x = (self.frame.size.width - boxCount * self.boxWidth - (boxCount - 1)* margin) * 0.5 + (self.boxWidth + margin) * i;
        UITextField *pwdLabel = [[UITextField alloc] initWithFrame:CGRectMake(x, (textFieldHeigh - self.boxWidth) * 0.5+CGRectGetMaxY(headLabel.frame), self.boxWidth, self.boxWidth)];
        pwdLabel.layer.borderColor = UIColorFromHex(0xdedede).CGColor;
        pwdLabel.enabled = NO;
        pwdLabel.textAlignment = NSTextAlignmentCenter;//居中
        pwdLabel.secureTextEntry = NO;//设置密码模式
        pwdLabel.layer.borderWidth = 1;
        [self addSubview:pwdLabel];
        
        [self.boxes addObject:pwdLabel];
    }
    //进入界面，contentTextField 成为第一响应
    [self.contentTextField becomeFirstResponder];
}
#pragma mark 文本框内容改变
- (void)txchange:(UITextField *)tx
{
    NSString *password = tx.text;

    for (int i = 0; i < self.boxes.count; i++)
    {
        UITextField *pwdtx = [self.boxes objectAtIndex:i];
        pwdtx.text = @"";
        if (i < password.length)
        {
            NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
            pwdtx.text = pwd;
        }
        
    }
    // 输入密码完毕
    if (password.length == boxCount)
    {
         [tx resignFirstResponder];//隐藏键盘
        if (self.returnPasswordStringBlock != nil) {
            self.returnPasswordStringBlock(password);
        }

    }
}
#pragma mark --- 懒加载
- (NSMutableArray *)boxes{
    if (!_boxes) {
        _boxes = [NSMutableArray array];
    }
    return _boxes;
}

- (void)setTitle:(NSString *)title {
    
    _title = title;
    self.headLabel.text = title;
}

@end
